package cn.lycode.generator.core.util;

import cn.lycode.generator.core.AbstractCodeGenerationTemplate;
import cn.lycode.generator.core.builder.java.*;
import cn.lycode.generator.core.builder.sql.MenuSqlCodeBuilder;
import cn.lycode.generator.core.builder.vue.FormCodeBuilder;
import cn.lycode.generator.core.builder.vue.IndexCodeBuilder;
import cn.lycode.generator.core.builder.vue.InterfaceCodeBuilder;
import cn.lycode.generator.core.builder.vue.ModulesCodeBuilder;
import cn.lycode.generator.domian.vo.GeneratorDetailVO;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.*;

/**
 * BuildApiTemplateUtils
 */
public class BuildTemplateUtils {

    /**
     * java模版
     *
     * @param configurer
     * @param rootPath
     * @param detailVO
     * @param model
     * @return
     */
    public static List<AbstractCodeGenerationTemplate> getApiTemplates(FreeMarkerConfigurer configurer,
                                                                       String rootPath,
                                                                       GeneratorDetailVO detailVO,
                                                                       Map<String, Object> model) {
        boolean hasImport = "1".equals(detailVO.getGeneratorInfo().getHasImport());
        List<AbstractCodeGenerationTemplate> list = new ArrayList<>(Arrays.asList(
                new ControllerCodeBuilder(configurer, rootPath, detailVO, model),
                new ServiceCodeBuilder(configurer, rootPath, detailVO, model),
                new ServiceImplCodeBuilder(configurer, rootPath, detailVO, model),
                new MapperCodeBuilder(configurer, rootPath, detailVO, model),
                new MapperXmlCodeBuilder(configurer, rootPath, detailVO, model),
                new DtoListCodeBuilder(configurer, rootPath, detailVO, model),
                new DtoCreateCodeBuilder(configurer, rootPath, detailVO, model),
                new DtoUpdateCodeBuilder(configurer, rootPath, detailVO, model)
        ));
        if (hasImport) list.add(new DtoImportCodeBuilder(configurer, rootPath, detailVO, model));
        list.add(new EntityCodeBuilder(configurer, rootPath, detailVO, model));
        list.add(new VoCodeBuilder(configurer, rootPath, detailVO, model));

        String generateType = detailVO.getGeneratorInfo().getGenerateType();
        switch (generateType) {
            case "all":
            case "server":
                return list;
            case "service":
                return Arrays.asList(
                        new EntityCodeBuilder(configurer, rootPath, detailVO, model),
                        new ServiceCodeBuilder(configurer, rootPath, detailVO, model),
                        new ServiceImplCodeBuilder(configurer, rootPath, detailVO, model),
                        new MapperCodeBuilder(configurer, rootPath, detailVO, model),
                        new MapperXmlCodeBuilder(configurer, rootPath, detailVO, model)

                );
            case "db":
                return Arrays.asList(
                        new EntityCodeBuilder(configurer, rootPath, detailVO, model),
                        new MapperCodeBuilder(configurer, rootPath, detailVO, model),
                        new MapperXmlCodeBuilder(configurer, rootPath, detailVO, model)
                );
            default:
                return Collections.emptyList();
        }
    }

    /**
     * web模版
     */
    public static List<AbstractCodeGenerationTemplate> getWebTemplates(FreeMarkerConfigurer configurer, String rootPath, GeneratorDetailVO detailVO, Map<String, Object> model) {
        List<AbstractCodeGenerationTemplate> list = new ArrayList<>(Arrays.asList(
                new IndexCodeBuilder(configurer, rootPath, detailVO, model),
                new FormCodeBuilder(configurer, rootPath, detailVO, model),
                new InterfaceCodeBuilder(configurer, rootPath, detailVO, model),
                new ModulesCodeBuilder(configurer, rootPath, detailVO, model)
        ));

        String generateType = detailVO.getGeneratorInfo().getGenerateType();

        switch (generateType) {
            case "all":
                return list;
            default:
                return Collections.emptyList();
        }
    }


    public static List<AbstractCodeGenerationTemplate> getSqlTemplates(FreeMarkerConfigurer configurer, String rootPath, GeneratorDetailVO detailVO, Map<String, Object> model) {
        String generateType = detailVO.getGeneratorInfo().getGenerateType();
        if (!Objects.equals(generateType, "all")) {
            return Collections.emptyList();
        }

        List<AbstractCodeGenerationTemplate> list = new ArrayList<>(Arrays.asList(
              new MenuSqlCodeBuilder(configurer, rootPath, detailVO, model)
        ));
        return list;
    }

}
